package com.changjiang.myproductsale.myproduct.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "ServletCode", value = "/code")
public class ServletCode extends HttpServlet {

    public BufferedImage createImage(StringBuilder codefinall) {
        long serialVersionUID = 1L;
        int WIDTH = 100;
        int HEIGHT = 40;
        int CODE_COUNT = 4;
        int FONT_HEIGHT = 30;
        char[] CODE_SEQUENCE = {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
        };
        // Create the image buffer
        BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

        // Fill background
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, WIDTH, HEIGHT);

        // Draw the border
        graphics.setColor(Color.BLACK);
        graphics.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);

        // Create font and set font style
        Font font = new Font("Fixedsys", Font.BOLD, FONT_HEIGHT);
        graphics.setFont(font);

        // Generate random code
        Random random = new Random();
        //codefinall= new StringBuilder(CODE_COUNT);
        for (int i = 0; i < CODE_COUNT; i++) {
            char code = CODE_SEQUENCE[random.nextInt(CODE_SEQUENCE.length)];
            codefinall.append(code);

            // Draw the character on the image
            graphics.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            graphics.drawString(String.valueOf(code),
                    (int)(i * 1.0 * WIDTH / CODE_COUNT),
                    (int)(HEIGHT / 1.5 + random.nextInt(10) - 5) );
        }
        graphics.dispose();
        return  bufferedImage;
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("image/jpeg"); //告之浏览器返回的是一张图片
        //变量1存验证码
        StringBuilder code=new StringBuilder(4);
        //变量2存图片
        BufferedImage codeImg=new BufferedImage(100,40,BufferedImage.TYPE_INT_RGB);

        codeImg=createImage(code); //调用方法获得验证码和图片
        System.out.println("生成验证码:"+code);
        request.getSession().setAttribute("code",code); //把生成验证码存入session

        //把图片显示出来
        ServletOutputStream out=response.getOutputStream(); //得到流
        ImageIO.write(codeImg,"jpeg",out); //以流的形式把图片显示出来
        //关闭
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
